<? include ("../init.php") ?>
<?
  $detail = $_POST['detail'];
  $date = $_POST['date'];
  

  $sql = "INSERT INTO news_promotion(news_date, news_detail,news_active) VALUES('$date','$detail',1)";

  $db->execute($sql);
  
  if(is_uploaded_file($_FILES["news_img"]["tmp_name"]) AND $_FILES["news_img"]["tmp_name"]!=''){
    $news_id = $db->lastInsertedId();
    
    $image = new image();
  
    ini_set("html_errors", "0");
    
    // Check the upload
    if (!isset($_FILES["news_img"]) || !is_uploaded_file($_FILES["news_img"]["tmp_name"]) || $_FILES["news_img"]["error"] != 0) {
      echo "ERROR:invalid upload";
      exit(0);
    }
  
    $name = get_file_name($news_id, $_FILES['news_img']['tmp_name']);
    $path = "../../images/news-promotion";
    $urlpath = get_url_path($path);
    $link = URL."/".$urlpath."/".$name;
    $filename = $path ."/". $name;
  
    if (!move_uploaded_file($_FILES['news_img']['tmp_name'], $filename)){
      echo 'Problem: Could not move file to destination directory';
      exit(0);
    }
  
    ini_set('memory_limit', '100M');
    $image->load($filename);
    $image->resize(260,300);
    $image->save($filename);
    ini_set('memory_limit', '16M');
  
    chmod($filename,0755);
    
    $sql = "UPDATE news_promotion SET news_img='$name' WHERE news_id=$news_id";
    $db->execute($sql);
  }
  
  header("Location:index.php");
?>